package com.example.apricots.Base;

import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;

import com.example.apricots.Common.Contracts;
import com.example.apricots.Services.NetService;
import com.example.apricots.bean.greendao.DaoMaster;
import com.example.apricots.bean.greendao.DaoSession;

public class App extends Application {
    private static App instance=null;
    private static Context mContext;

    public static App getInstance() {
        return instance;
    }

    private DaoMaster.DevOpenHelper mHelper;
    private DaoMaster daoMaster;
    private DaoSession daosession;
    private SQLiteDatabase db;

    public static Context getContext() {
        return mContext;
    }

    /**
     * greenDao 相关
     */
    private void setGreenDao() {
        //通过DaoMaster内部类DevOpenHelper可以获取一个SQLiteOpenHelper 对象
        mHelper = new DaoMaster.DevOpenHelper(this, Contracts.DB_NAME,null);
        db = mHelper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daosession = daoMaster.newSession();
    }

    public DaoSession getDaosession() {
        return daosession;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
        initService();
    }


    /**
     * 初始化服务
     */
    private void initService() {
        //greendao相关
        setGreenDao();
        //注册全局网络监听需要动态注册
        netservice();
    }

    private void netservice() {
        IntentFilter filter=new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        NetService service=new NetService();
        registerReceiver(service,filter);
    }

}
